﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTspcAppProject.Model;
using ModBusTcp;
using HTDAL.HTSPC_DBDataSetTableAdapters;
using System.Data;

namespace HTspcAppProject
{
    class DataPacket
    {
        
        List<float> datF;
        public DataPacket()
        {
            Wrapper = ModBusWrapper.CreateInstance(Protocol.TCPIP);
            //devSetTa = new ht_devSetTableAdapter();
            datF = new List<float>();
        }
        public ModBusWrapper Wrapper = null;
        public bool DevDataGet()
        {
            byte[] data = null;
            datF.Clear();
            try
            {
                data = this.Wrapper.Receive();
                //data = new byte[320];
            }
            catch (Exception ex)
            {
                //data = new byte[320];
                CommonClass.LogError(ex.ToString());
                return false;
            }
            int DataFLenght = data.Length / 4;

            if(data!=null)
            for (int i = 0; i + 3 < data.Length; i = i + 4)
            {
                datF.Add(BitConverter.ToSingle(new byte[]{data[i+1],data[i],data[i+3],data[i+2]},0));
            }
            if (datF.Count > 0)
            {
                DoPacket(datF);
            }
            else
            {
                return false;
            }
            return true;
        }

        Random ra = new Random();

        public Dictionary<string, ht_pmmr> DoPacket(List<float> DataF)
        {
            Dictionary<int, string[]> chDict = CommonClass.chDict;
            foreach (int chId in chDict.Keys)
            {
                string[] devInf = chDict[chId];

                if (!CommonClass.pmDict.Keys.Contains(devInf[0]))
                {
                    continue;
                }
                ht_pmmr pmmr = CommonClass.pmDict[devInf[0]];
                pmmr.dt = DateTime.Now;
                if (chId <= DataF.Count)
                    switch (devInf[1])
                    {
                        case "后室温度":
                            //pmmr.hswdCur = Convert.ToDouble(DataF[chId - 1]);
                            pmmr.hswdCur =  ra.Next(400, 950);
                            break;
                        case "油槽温度":
                            //pmmr.ycwdCur = Convert.ToDouble(DataF[chId - 1]);
                            pmmr.ycwdCur = ra.Next(80, 100); 
                            break;
                        case "CP值": 
                            //pmmr.cpCur = Convert.ToDouble(DataF[chId - 1]);
                            pmmr.cpCur = (float)ra.Next(0, 200) / (float)100;
                            break;
                        case "CMV阀开度":
                            //pmmr.CMV = Convert.ToDouble(DataF[chId - 1]);
                            pmmr.CMV = (float)ra.Next(0, 200) / (float)100;
                            break;
                        case "油位":
                            //pmmr.tlCur = Convert.ToDouble(DataF[chId - 1]);
                            pmmr.tlCur = (float)ra.Next(0, 200) / (float)100;
                            break;
                        case "流量":
                            //pmmr.RxCur = Convert.ToDouble(DataF[chId - 1]);
                            pmmr.RxCur = (float)ra.Next(0, 200) / (float)100;
                            break;
                        default: continue;
                    }
                //lock (CommonClass.devDtList)
                //{
                //    ht_devSetTableAdapter devSetTa =new ht_devSetTableAdapter();
                //    CommonClass.devDtList = devSetTa.GetData();
                //}
                CommonClass.pmDict[devInf[0]] = pmmr;
            }
            return CommonClass.pmDict;
        }
    }
}
